/*
 * Copyright 2011 es-planning Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.arctos.ursus.extension.net;

import java.io.IOException;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * Server socket session.
 * <p>
 * Handles transmission using TCP server.
 * </p>
 * 
 */
class ServerSocketSession extends Session {

	/** Server socket channel. */
    ServerSocketChannel channel;

    /** Session handler. */
    ServerSocketSessionHandler sessionHandler;

    /**
     * Constructor.
     * 
     * @param service Service
     * @param channel Channel
     * @param selectorHandler Selector handler
     * @throws IOException
     */
    ServerSocketSession(Service service, ServerSocketChannel channel, SelectorHandler selectorHandler) throws IOException {
        super(service, selectorHandler);

        this.channel = channel;
        
        sessionHandler = new ServerSocketSessionHandler(this);
    }

    /**
     * {@inheritDoc}
     * 
     */
    @Override
    void doClose() throws IOException {
        try {
            channel.close();
        } finally {
            selectorHandler.cancel(channel);
            service.sessionClosed(this);
        }
    }

    /**
     * Get session handler of this session.
     * 
     * @return Session handler
     */
    ServerSocketSessionHandler getSessionHandler() {
        return sessionHandler;
    }

    /**
     * Accept connection.
     * 
     * @return Socket channel that accepted the connection // TODO
     * @throws IOException
     */
    SocketChannel accept() throws IOException {
        return channel.accept();
    }

    /**
     * {@inheritDoc}
     * 
     */
    @Override
    SocketAddress getLocalAddress() {
        return channel.socket().getLocalSocketAddress();
    }

    /**
     * Get value of SO_RCVBUF socket option.
     * 
     * @return SO_RCVBUF Socket option
     * @throws SocketException 
     * @see java.net.ServerSocket#getReceiveBufferSize()
     */
    int getReceiveBufferSize() throws SocketException {
        return channel.socket().getReceiveBufferSize();
    }

    /**
     * Get value of SO_REUSEADDR socket option.
     * 
     * @return SO_REUSEADDR Socket option
     * @throws SocketException 
     * @see java.net.ServerSocket#getReuseAddress()
     */
    boolean getReuseAddress() throws SocketException {
        return channel.socket().getReuseAddress();
    }

    /**
     * Get value of SO_TIMEOUT socket option.
     * 
     * @return SO_TIMEOUT Socket option
     * @throws IOException 
     * @see java.net.ServerSocket#getSoTimeout()
     */
    int getSoTimeout() throws IOException {
        return channel.socket().getSoTimeout();
    }

    /**
     * Configure SO_TIMEOUT socket option.
     * 
     * @param timeout SO_TIMEOUT Socket option
     * @throws SocketException 
     * @see java.net.ServerSocket#setSoTimeout(int)
     */
    void setSoTimeout(int timeout) throws SocketException {
        channel.socket().setSoTimeout(timeout);
    }
}
